<!DOCTYPE html>
<html>
<head>
    <title>SoftUni Students</title>
    <style>
        label {
            display: inline-block;
            margin: 5px 0;
        }
    </style>
</head>
<body>
<form action="task4.php" method="get">
    <label for="column">Sort By:</label>
    <select name="column" id="column">
        <option value="id">id</option>
        <option value="username">username</option>
        <option value="result">result</option>
    </select><br>
    Show:
    <input type="radio" id="asc" name="order" value="ascending" checked="checked" />
    <label for="asc">Ascending</label>
    <input type="radio" id="desc" name="order" value="descending"/>
    <label for="desc">Descending</label><br>
    <label for="students">Students:</label><br>
    <textarea name="students" id="students" cols="50" rows="10">Pesho, pesho.g@gmail.com, onsite, 400
        Mariika, mariika@gmail.com, online, 350
        Geri, geri@gmail.com, online, 50
        Pesho, peshkata@gmail.com, onsite, 0
        Gosho & Kiro, gosho@gmail.com, onsite, 400
        Mincho, praznikov@vremeto.bg, online, 50</textarea><br>
    <input type="submit" value="Submit"/>
</form>
<?php
if (isset($_GET['students']) && isset($_GET['column']) && isset($_GET['order'])) {
    $students = explode("\n", $_GET['students']);
    $sortBy = $_GET['column']; // id username result
    $sortOrder = $_GET['order']; // ascending   descending

    class Student {
        public $id;
        public $username;
        public $email;
        public $status;
        public $score;

        function Student($id, $username, $mail, $status, $score) {
            $this->id = $id;
            $this->username = $username;
            $this->email = $mail;
            $this->status = $status;
            $this->score = $score;
        }
    }

    $allStudents = array();
    $id = 1;

    foreach ($students as $student) {
        $studentParams = explode(", ", $student);
        $newStudent = new Student($id, $studentParams[0], $studentParams[1], $studentParams[2], $studentParams[3]);
        $allStudents[] = $newStudent;
        $id++;
    }

    function sortByUsername($a, $b) {
        if ($a->username < $b->username) {
            return -1;
        }
        else if ($a->username > $b->username) {
            return 1;
        }
        else {
            return 0;
        }
    }

    function sortByScore($a, $b) {
        if ($a->score === $b->score) {
            return 0;
        }

        return $a->score < $b->score;
    }

    //if ($sortBy === 'username') {
    //    usort($allStudents, 'sortByUsername');
    //}
    //else if ($sortBy === 'result') {
    //    usort($allStudents, 'sortByScore');
    //}

    //if ($sortOrder === 'descending') {
    //    $allStudents = array_reverse($allStudents, true);
    //}

    //if ($sortBy === 'result') {
    //    $zeroStudents = array();

    //    $indexer = 0;

    //    foreach ($allStudents as $student) {
    //        if ($student->score === 0) {
    //            array_push($zeroStudents, $student);
    //        }
    //    }

    //    for ($i = 0; $i < count($allStudents); $i++) {
    //        if ($allStudents[$i]->score === 0) {
    //            unset($allStudents[$i]);
    //        }
    //    }

    //    if ($sortOrder === 'ascending') {
    //        foreach ($zeroStudents as $zeroStudent) {

    //            array_push($allStudents, $zeroStudent);
    //        }
    //    }
    //    else {
    //        foreach ($zeroStudents as $zeroStudent) {
    //            array_unshift($allStudents , $zeroStudent);
    //        }
    //    }
    //}

    $result = '<table><thead><tr><th>Id</th><th>Username</th><th>Email</th><th>Type</th><th>Result</th></tr></thead>';

    foreach ($allStudents as $student) {
        $result .= "<tr>";

        $result .= "<td>" . htmlspecialchars($student->id) . "</td>" .
            "<td>" . htmlspecialchars($student->username) . "</td>" .
            "<td>" . htmlspecialchars($student->email) . "</td>" .
            "<td>" . htmlspecialchars($student->status) . "</td>" .
            "<td>" . htmlspecialchars($student->score) . "</td>";

        $result .= "</tr>";
    }

    $result .= "</table";

    echo $result;
}
?>
</body>
</html>